package rewards.testdb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.EmptyResultDataAccessException;

import rewards.Dining;
import rewards.domain.account.Account;
import rewards.domain.restaurant.BenefitAvailabilityPolicy;
import rewards.domain.restaurant.Restaurant;
import rewards.domain.restaurant.RestaurantRepository;
import common.money.Percentage;

/**
 * A dummy restaurant repository implementation. Has a single restaurant
 * "Apple Bees" with a 8% benefit availability percentage that's always
 * available.
 * 
 * Stubs facilitate unit testing. An object needing a RestaurantRepository can
 * work with this stub and not have to bring in expensive and/or complex
 * dependencies such as a Database. Simple unit tests can then verify object
 * behavior by considering the state of this stub.
 */
public class StubRestaurantRepository implements RestaurantRepository {

	private Map<String, Restaurant> restaurantsByMerchantNumber = new HashMap<String, Restaurant>();

	public StubRestaurantRepository() {
		Restaurant restaurant = new Restaurant("1234567890", "Apple Bees");
		restaurant.setBenefitPercentage(Percentage.valueOf("8%"));
		restaurant.setBenefitAvailabilityPolicy(new AlwaysReturnsTrue());
		restaurantsByMerchantNumber.put(restaurant.getNumber(), restaurant);
	}

	public Restaurant findByMerchantNumber(String merchantNumber) {
		Restaurant restaurant = (Restaurant) restaurantsByMerchantNumber
				.get(merchantNumber);
		if (restaurant == null) {
			throw new EmptyResultDataAccessException(1);
		}
		return restaurant;
	}

	public List<Restaurant> getAllRestaurants() {
		return new ArrayList<Restaurant>(restaurantsByMerchantNumber.values());
	}

	/**
	 * A simple "dummy" benefit availability policy that always returns true.
	 * Only useful for testing--a real availability policy might consider many
	 * factors such as the day of week of the dining, or the account's reward
	 * history for the current month.
	 */
	private static class AlwaysReturnsTrue implements BenefitAvailabilityPolicy {
		public boolean isBenefitAvailableFor(Account account, Dining dining) {
			return true;
		}

		public char code() {
			return 'A';
		}
	}
}